//
//  PaperQuestionView.m
//  JW_BasePorject
//
//  Created by JW on 2018/6/27.
//  Copyright © 2018年 JW. All rights reserved.
//

#import "PaperQuestionView.h"
#import "PaperOptionView.h"

@interface PaperQuestionView ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic,strong)NSMutableArray *selectIndexPaths;

@property(nonatomic,strong) NSIndexPath *selectPath;

@property (nonatomic,copy) NSString *userResult;

@end

@implementation PaperQuestionView

-(instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style
{
    if (self = [super initWithFrame:frame style:style]) {
        [self initQuestionView];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        
        [self initQuestionView];
    }
    return self;
}

- (void)initQuestionView{
    self.delegate = self;
    self.dataSource = self;
//    self.scrollEnabled = NO;
    self.backgroundColor = [UIColor whiteColor];
    self.separatorStyle = UITableViewCellSeparatorStyleNone;
}



#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return 5;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    PaperOptionView *cell = [PaperOptionView cellWithTableView:tableView];
    cell.backgroundColor = [UIColor yellowColor];
//    cell.textLabel.text = [NSString stringWithFormat:@"%@ - %ld", indexPath.row % 2?@"push":@"modal",indexPath.row];
    
    return cell;
}

#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 100;
    
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    return  [UIView new];
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    NSLog(@"点击了第%ld个cell",(long)indexPath.row);
    PaperOptionView *cell = [tableView cellForRowAtIndexPath:indexPath];
    if (indexPath.row % 2) {//单选
        int newRow = (int)indexPath.row;
        int oldRow = (int)(_selectPath != nil) ? (int)_selectPath.row : -1;
        if(newRow !=  oldRow) { //selectedButton : 我这里是cell中得一个按钮属性
//            AlterSelectedTableViewCell *newCell = (AlterSelectedTableViewCell *)[tableView cellForRowAtIndexPath:indexPath];
//            newCell.selectedButton.selected=YES; //yes:打勾状态
            
//            AlterSelectedTableViewCell *oldCell = (AlterSelectedTableViewCell *)[tableView cellForRowAtIndexPath:_selectPath]; //当_selectPath为-1时,返回cell == nil
//            oldCell.selectedButton.selected = NO; //no:取消打勾
            _selectPath= [indexPath copy];//一定要这么写，要不报错
        }
        

        
    }else{//多选
        if([self.selectIndexPaths containsObject:indexPath]){ //在NSMutableArray中用（bool）类型的containsObject判断这个对象是否存在这个数组中（判断的是内存地址）contains：包含
            //存在以选中的，就执行（为真就执行）把存在的移除
            [self.selectIndexPaths removeObject:indexPath];  //把这个cell的标记移除
        } else{//不存在这个标记，那点击后就添加到这个数组中
            [self.selectIndexPaths addObject:indexPath];
        }
        
        [tableView reloadRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationFade];//重新刷新这行
        
    }
    

    
}

@end
